<?php
namespace App\Api\Controllers;

use App\Http\Controllers\Controller;
use App\Api\Models\CmcSignIn;
use Illuminate\Http\Request;
use App\Api\Services\JsonService;
use Illuminate\Support\Facades\Redis;


class CmcSignInController extends Controller
{

    /**
     * 每日签到
     * @param Request $request
     * @return array
     */
    public function getSignIn(Request $request)
    {
        $validate = $request->validate([
            'uid' => ['required', 'alpha_num']
        ]);

        $validate['add_time'] = date('Y年m月d日', time());

        //调用模型层
        $signInRes = CmcSignIn::signInData($validate);

        $signInRedis = Redis::hMget(date('Y-m-d', time()), ['uid', 'date', 'integral']);

        if ($signInRes && $signInRedis) {
            return JsonService::fail('今日已签到，请不要重复签到');
        }

        $integral = count($validate) * 10;

        if ($integral > 70) {
            $integral = 70;
        }

        $validate['integral'] = (int)$integral;

        $addSignIn = CmcSignIn::signInadd($validate);

        Redis::hMSet(date('Y-m-d', time()), ['uid' => $validate['uid'], 'date' => date('Y-m-d', time()), 'integral' => $integral]);

        if ($addSignIn) {
            return JsonService::success('签到成功');
        }
        return JsonService::fail('签到失败');
    }
}